package com.dmzc.stage3.Unsafe;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/***
 * 直接分配一个实例，不会调用构造器
 */
public class Test1 {
    public static void main(String[] args) throws Exception{
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        Unsafe unsafe = (Unsafe) f.get(null);
        A o1=new A();
        System.out.println(o1.a());
        A o2=A.class.newInstance();
        System.out.println(o2.a());
        A o3=(A)unsafe.allocateInstance(A.class);
        System.out.println(o3.a());
    }

    static class A {
        private long a;

        public A() {
            this.a = 1;
        }

        public long a() {
            return this.a;
        }
    }
}

